package cate.game.activity.hangdrop.po;

import cate.common.table.activity.hangdrop.shop.HangDropActivityShopRow;
import cate.common.table.d.GDFunc;
import cate.common.util.GameResult;
import cate.game.activity.base.core.SubActivitySingle;
import cate.game.activity.hangdrop.HangDropActivity;
import cate.game.res.MixRes;
import cate.game.res.MixResAdder;
import com.fasterxml.jackson.annotation.JsonIgnore;
import easy.java.dev.note.NoteClass;
import easy.java.dev.note.NoteField;
import easy.java.practice.errorcode.EcResult;

import java.util.HashMap;
import java.util.Map;

@NoteClass("限时兑换-限购礼包")
public class HangDropShopPO extends SubActivitySingle<HangDropActivity> {
    @NoteField("配置ID => 购买数量")
    public Map<Integer, Integer> buyRecords;

    @Override
    public void initialize(HangDropActivity parent) {
        super.initialize(parent);
        if (buyRecords == null) {
            buyRecords = new HashMap<>();
        }
    }

    public void excelCheck() {
        Map<Integer, HangDropActivityShopRow> configs = getShopConfigs();
        if (configs == null || configs.isEmpty()) {
            buyRecords.clear();
            return;
        }
        buyRecords.entrySet().removeIf(kv -> !configs.containsKey(kv.getKey()));
        for (HangDropActivityShopRow config : configs.values()) {
            if (!buyRecords.containsKey(config.id)) {
                buyRecords.put(config.id, 0);
            }
        }
    }

    public void clear() {
        buyRecords.clear();
    }

    public int configGroup() {
        return parent.getGroup();
    }

    @Override
    public void onDaySpan(boolean silence) {
        super.onDaySpan(silence);
        buyRecords.entrySet().forEach(kv -> {
            HangDropActivityShopRow config = getShopConfig(kv.getKey());
            if (config != null && config.dailyReset == 1) {
                kv.setValue(0);
            }
        });
    }

    /**
     * 获取礼包配置
     */
    @JsonIgnore
    public HangDropActivityShopRow getShopConfig(int tid) {
        switch (funcId()) {
            case GDFunc.ACT_HANG_DROP:
                return role.getGame().table.hangDropActivity.shop.get(tid);
            default:
                return null;
        }
    }

    /**
     * 获取全部兑换配置
     */
    @JsonIgnore
    public Map<Integer, HangDropActivityShopRow> getShopConfigs() {
        Map<Integer, HangDropActivityShopRow> ret = new HashMap<>();
        switch (funcId()) {
            case GDFunc.ACT_HANG_DROP: {
                for (HangDropActivityShopRow config : role.getGame().table.hangDropActivity.shop.getList()) {
                    if (config.group == configGroup()) {
                        ret.put(config.id, config);
                    }
                }
            }
            break;
        }
        return ret;
    }

    /**
     * 是否支持支付
     *
     * @param tid 配置ID
     * @return 检测结果 无携带数据
     */
    public GameResult<Void> allowBuy(int tid) {
        GameResult<Void> r = new GameResult<>();
        HangDropActivityShopRow config = getShopConfig(tid);
        if (config == null) {
            return r.fail("商品不存在");
        }
        if (!buyRecords.containsKey(tid)) {
            return r.fail("商品不存在");
        }
        if (config.type != 3) {
            return r.fail("不支持支付");
        }
        int curNum = buyRecords.get(tid);
        if (config.limit != -1 && curNum > config.limit - 1) {
            return r.fail("限购数量不足");
        }
        return r.success();
    }

    /**
     * 购买礼包
     *
     * @param tid 配置ID
     * @param num 数量
     * @return 购买结果 无携带数据
     */
    public EcResult<Void> doBuyGift(int tid, int num) {
        EcResult<Void> r = new EcResult<>();
        if (num <= 0) {
            return r.fail("???");
        }
        HangDropActivityShopRow config = getShopConfig(tid);
        if (config == null) {
            return r.fail("商品不存在");
        }
        if (!buyRecords.containsKey(tid)) {
            return r.fail("商品不存在");
        }
        if (config.type != 1 && config.type != 2) {
            return r.fail("不支持购买");
        }
        int curNum = buyRecords.get(tid);
        if (config.limit != -1 && curNum > config.limit - num) {
            return r.fail("限购数量不足");
        }
        if (config.type == 2) {
            MixRes consumeRes = new MixRes(config.consumeStr);
            consumeRes.items.forEach(item -> item.num *= num);
            EcResult<?> r_res = consumeRes.consumeCheck(role);
            if (!r_res.ok()) {
                return r.fail(r_res.message);
            }
            role.log.setOperation(parent.getGDOperation(), tid);
            consumeRes.consume(role);
            role.log.clearOperation();
        }
        supplyGifts(tid, num);
        return r.success();
    }

    /**
     * 发放商品道具
     *
     * @param tid 配置ID
     * @param num 数量
     */
    public void supplyGifts(int tid, int num) {
        HangDropActivityShopRow config = getShopConfig(tid);
        if (config == null) {
            return;
        }
        if (!buyRecords.containsKey(tid)) {
            return;
        }
        buyRecords.put(tid, buyRecords.get(tid) + num);
        MixRes rewardRes = new MixRes(config.rewardStr);
        rewardRes.items.forEach(item -> item.num *= num);
        EcResult exe = new MixResAdder().setRes(rewardRes)
                .setOperation(parent.getGDOperation())
                .setOperationSub(tid)
                .setSendMailWhenFull(true)
                .setDialogReward(true)
                .exe(role);
    }
}
